home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / doc / interpreter / var.tex < prev    next >
Text File  |  1997-08-13  |  15KB  |  566 lines

  1. @c Copyright (C) 1996, 1997 John W. Eaton
  2. @c This is part of the Octave manual.
  3. @c For copying conditions, see the file gpl.tex.
  4.  
  5. @node Variables, Expressions, Data Structures, Top
  6. @chapter Variables
  7. @cindex variables, user-defined
  8. @cindex user-defined variables
  9.  
  10. Variables let you give names to values and refer to them later.  You have
  11. already seen variables in many of the examples.  The name of a variable
  12. must be a sequence of letters, digits and underscores, but it may not begin
  13. with a digit.  Octave does not enforce a limit on the length of variable
  14. names, but it is seldom useful to have variables with names longer than
  15. about 30 characters.  The following are all valid variable names
  16.  
  17. @cindex job hunting
  18. @cindex getting a good job
  19. @cindex flying high and fast
  20. @example
  21. @group
  22. x
  23. x15
  24. __foo_bar_baz__
  25. fucnrdthsucngtagdjb
  26. @end group
  27. @end example
  28.  
  29. @noindent
  30. However, names like @code{__foo_bar_baz__} that begin and end with two
  31. underscores are understood to be reserved for internal use by Octave.
  32. You should not use them in code you write, except to access Octave's
  33. documented internal variables and built-in symbolic constants.
  34.  
  35. Case is significant in variable names.  The symbols @code{a} and
  36. @code{A} are distinct variables.
  37.  
  38. A variable name is a valid expression by itself.  It represents the
  39. variable's current value.  Variables are given new values with
  40. @dfn{assignment operators} and @dfn{increment operators}.
  41. @xref{Assignment Ops, ,Assignment Expressions}.
  42.  
  43. A number of variables have special built-in meanings.  For example,
  44. @code{PWD} holds the current working directory, and @code{pi} names the
  45. ratio of the circumference of a circle to its diameter. @xref{Summary of
  46. Built-in Variables}, for a list of all the predefined variables.  Some
  47. of these built-in symbols are constants and may not be changed.  Others
  48. can be used and assigned just like all other variables, but their values
  49. are also used or changed automatically by Octave.
  50.  
  51. Variables in Octave do not have fixed types, so it is possible to first
  52. store a numeric value in a variable and then to later use the same name
  53. to hold a string value in the same program.  Variables may not be used
  54. before they have been given a value.  Doing so results in an error.
  55.  
  56. @menu
  57. * Global Variables::            
  58. * Status of Variables::         
  59. * Summary of Built-in Variables::  
  60. * Defaults from the Environment::  
  61. @end menu
  62.  
  63. @node Global Variables, Status of Variables, Variables, Variables
  64. @section Global Variables
  65. @cindex global variables
  66. @cindex @code{global} statement
  67. @cindex variables, global
  68.  
  69. A variable that has been declared @dfn{global} may be accessed from
  70. within a function body without having to pass it as a formal parameter.
  71.  
  72. A variable may be declared global using a @code{global} declaration
  73. statement.  The following statements are all global declarations.
  74.  
  75. @example
  76. @group
  77. global a
  78. global b = 2
  79. global c = 3, d, e = 5
  80. @end group
  81. @end example
  82.  
  83. It is necessary declare a variable as global within a function body in
  84. order to access it.  For example,
  85.  
  86. @example
  87. @group
  88. global x
  89. function f ()
  90.   x = 1;
  91. endfunction
  92. f ()
  93. @end group
  94. @end example
  95.  
  96. @noindent
  97. does @emph{not} set the value of the global variable @code{x} to 1.  In
  98. order to change the value of the global variable @code{x}, you must also
  99. declare it to be global within the function body, like this
  100.  
  101. @example
  102. @group
  103. function f ()
  104.   global x;
  105.   x = 1;
  106. endfunction
  107. @end group
  108. @end example
  109.  
  110. Passing a global variable in a function parameter list will
  111. make a local copy and not modify the global value.  For example, given
  112. the function
  113.  
  114. @example
  115. @group
  116. function f (x)
  117.   x = 0
  118. endfunction
  119. @end group
  120. @end example
  121.  
  122. @noindent
  123. and the definition of @code{x} as a global variable at the top level,
  124.  
  125. @example
  126. global x = 13
  127. @end example
  128.  
  129. @noindent
  130. the expression
  131.  
  132. @example
  133. f (x)
  134. @end example
  135.  
  136. @noindent
  137. will display the value of @code{x} from inside the function as 0,
  138. but the value of @code{x} at the top level remains unchanged, because
  139. the function works with a @emph{copy} of its argument.
  140.  
  141. @defvr {Built-in Variable} warn_comma_in_global_decl
  142. If the value of @code{warn_comma_in_global_decl} is nonzero, a
  143. warning is issued for statements like
  144.  
  145. @example
  146. global a = 1, b
  147. @end example
  148.  
  149. @noindent
  150. which makes the variables @code{a} and @code{b} global and assigns the
  151. value 1 to the variable @code{a}, because in this context, the comma is
  152. not interpreted as a statement separator.
  153.  
  154. The default value of @code{warn_comma_in_global_decl} is nonzero.
  155. @end defvr
  156.  
  157. @deftypefn {Built-in Function} {} is_global (@var{name})
  158. Return 1 if @var{name} is globally visible.  Otherwise, return 0.  For
  159. example,
  160.  
  161. @example
  162. @group
  163. global x
  164. is_global ("x")
  165.      @result{} 1
  166. @end group
  167. @end example
  168. @end deftypefn
  169.  
  170. @node Status of Variables, Summary of Built-in Variables, Global Variables, Variables
  171. @section Status of Variables
  172.  
  173. @deffn {Command} clear options pattern @dots{}
  174. Delete the names matching the given patterns from the symbol table.  The
  175. pattern may contain the following special characters:
  176. @table @code
  177. @item ?
  178. Match any single character.
  179.  
  180. @item *
  181. Match zero or more characters.
  182.  
  183. @item [ @var{list} ]
  184. Match the list of characters specified by @var{list}.  If the first
  185. character is @code{!} or @code{^}, match all characters except those
  186. specified by @var{list}.  For example, the pattern @samp{[a-zA-Z]} will
  187. match all lower and upper case alphabetic characters. 
  188. @end table
  189.  
  190. For example, the command
  191.  
  192. @example
  193. clear foo b*r
  194. @end example
  195.  
  196. @noindent
  197. clears the name @code{foo} and all names that begin with the letter
  198. @code{b} and end with the letter @code{r}.
  199.  
  200. If @code{clear} is called without any arguments, all user-defined
  201. variables (local and global) are cleared from the symbol table.  If
  202. @code{clear} is called with at least one argument, only the visible
  203. names matching the arguments are cleared.  For example, suppose you have
  204. defined a function @code{foo}, and then hidden it by performing the
  205. assignment @code{foo = 2}.  Executing the command @kbd{clear foo} once
  206. will clear the variable definition and restore the definition of
  207. @code{foo} as a function.  Executing @kbd{clear foo} a second time will
  208. clear the function definition.
  209.  
  210. This command may not be used within a function body.
  211. @end deffn
  212.  
  213. @deffn {Command} who options pattern @dots{}
  214. @deffnx {Command} whos options pattern @dots{}
  215. List currently defined symbols matching the given patterns.  The
  216. following are valid options.  They may be shortened to one character but
  217. may not be combined.
  218.  
  219. @table @code
  220. @item -all
  221. List all currently defined symbols.
  222.  
  223. @item -builtins
  224. List built-in variables and functions.  This includes all currently
  225. compiled function files, but does not include all function files that
  226. are in the @code{LOADPATH}.
  227.  
  228. @item -functions
  229. List user-defined functions.
  230.  
  231. @item -long
  232. Print a long listing including the type and dimensions of any symbols.
  233. The symbols in the first column of output indicate whether it is
  234. possible to redefine the symbol, and whether it is possible for it to be
  235. cleared.
  236.  
  237. @item -variables
  238. List user-defined variables.
  239. @end table
  240.  
  241. Valid patterns are the same as described for the @code{clear} command
  242. above.  If no patterns are supplied, all symbols from the given category
  243. are listed.  By default, only user defined functions and variables
  244. visible in the local scope are displayed.
  245.  
  246. The command @kbd{whos} is equivalent to @kbd{who -long}.
  247. @end deffn
  248.  
  249. @deftypefn {Built-in Function} {} exist (@var{name})
  250. Return 1 if the name exists as a variable, 2 if the name (after
  251. appending @samp{.m}) is a function file in the path, 3 if the name is a
  252. @samp{.oct} file in the path, or 5 if the name is a built-in function.
  253. Otherwise, return 0.
  254. @end deftypefn
  255.  
  256. @deftypefn {Built-in Function} {} document (@var{symbol}, @var{text})
  257. Set the documentation string for @var{symbol} to @var{text}.
  258. @end deftypefn
  259.  
  260. @deffn {Command} type options name @dots{}
  261. Display the definition of each @var{name} that refers to a function.
  262.  
  263. Normally also displays if each @var{name} is user-defined or builtin;
  264. the @code{-q} option suppresses this behaviour.
  265.  
  266. Currently, Octave can only display functions that can be compiled
  267. cleanly, because it uses its internal representation of the function to
  268. recreate the program text.
  269.  
  270. Comments are not displayed because Octave's parser currently discards
  271. them as it converts the text of a function file to its internal
  272. representation.  This problem may be fixed in a future release.
  273. @end deffn
  274.  
  275. @deffn {Command} which name @dots{}
  276. Display the type of each @var{name}.  If @var{name} is defined from a
  277. function file, the full name of the file is also displayed.
  278. @end deffn
  279.  
  280. @node Summary of Built-in Variables, Defaults from the Environment, Status of Variables, Variables
  281. @section Summary of Built-in Variables
  282.  
  283. Here is a summary of all of Octave's built-in variables along with
  284. cross references to additional information and their default values.  In
  285. the following table @var{octave-home} stands for the root directory
  286. where all of Octave is installed (the default is @file{@value{OCTAVEHOME}},
  287. @var{version} stands for the Octave version number (for example,
  288. @value{VERSION}) and @var{arch} stands for the type of system for which
  289. Octave was compiled (for example, @code{@value{TARGETHOSTTYPE}}).
  290.  
  291. @vtable @code
  292. @item EDITOR
  293. @xref{Commands For History}.
  294.  
  295. Default value: @code{"emacs"}.
  296.  
  297. @item EXEC_PATH
  298. @xref{Controlling Subprocesses}.
  299.  
  300. Default value: @code{":$PATH"}.
  301.  
  302. @item INFO_FILE
  303. @xref{Getting Help}.
  304.  
  305. Default value: @code{"@var{octave-home}/info/octave.info"}.
  306.  
  307. @item INFO_PROGRAM
  308. @xref{Getting Help}.
  309.  
  310. Default value: @code{"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info"}.
  311.  
  312. @item LOADPATH
  313. @xref{Function Files}.
  314.  
  315. Default value: @code{".:@var{octave-home}/lib/@var{version}"}.
  316.  
  317. @item OCTAVE_HOME
  318.  
  319. Default value: @code{"@value{OCTAVEHOME}"}.
  320.  
  321. @item PAGER
  322. @xref{Input and Output}.
  323.  
  324. Default value: @code{"less", or "more"}.
  325.  
  326. @item PS1
  327. @xref{Customizing the Prompt}.
  328.  
  329. Default value: @code{"\s:\#> "}.
  330.  
  331. @item PS2
  332. @xref{Customizing the Prompt}.
  333.  
  334. Default value: @code{"> "}.
  335.  
  336. @item PS4
  337. @xref{Customizing the Prompt}.
  338.  
  339. Default value: @code{"+ "}.
  340.  
  341. @item automatic_replot
  342. @xref{Two-Dimensional Plotting}.
  343.  
  344. Default value: 0.
  345.  
  346. @item beep_on_error
  347. @xref{Error Handling}.
  348.  
  349. Default value: 0.
  350.  
  351. @item completion_append_char
  352. @xref{Commands For Completion}.
  353.  
  354. Default value: @code{" "}.
  355.  
  356. @item default_eval_print_flag
  357. @xref{Evaluation}.
  358.  
  359. Default value: 1.
  360.  
  361. @item default_return_value
  362. @xref{Multiple Return Values}.
  363.  
  364. Default value: @code{[]}.
  365.  
  366. @item default_save_format
  367. @xref{Simple File I/O}.
  368.  
  369. Default value: @code{"ascii"}.
  370.  
  371. @item do_fortran_indexing
  372. @xref{Index Expressions}.
  373.  
  374. Default value: 0.
  375.  
  376. @item define_all_return_values
  377. @xref{Multiple Return Values}.
  378.  
  379. Default value: 0.
  380.  
  381. @item empty_list_elements_ok
  382. @xref{Empty Matrices}.
  383.  
  384. Default value: @code{"warn"}.
  385.  
  386. @item gnuplot_binary
  387. @xref{Three-Dimensional Plotting}.
  388.  
  389. Default value: @code{"gnuplot"}.
  390.  
  391. @item history_file
  392. @xref{Commands For History}.
  393.  
  394. Default value: @code{"~/.octave_hist"}.
  395.  
  396. @item history_size
  397. @xref{Commands For History}.
  398.  
  399. Default value: 1024.
  400.  
  401. @item ignore_function_time_stamp
  402. @xref{Function Files}.
  403.  
  404. Default value: @code{"system"}.
  405.  
  406. @item implicit_str_to_num_ok
  407. @xref{String Conversions}.
  408.  
  409. Default value: 0.
  410.  
  411. @item ok_to_lose_imaginary_part
  412. @xref{Special Utility Matrices}.
  413.  
  414. Default value: @code{"warn"}.
  415.  
  416. @item output_max_field_width
  417. @xref{Matrices}.
  418.  
  419. Default value: 10.
  420.  
  421. @item output_precision
  422. @xref{Matrices}.
  423.  
  424. Default value: 5.
  425.  
  426. @item page_screen_output
  427. @xref{Input and Output}.
  428.  
  429. Default value: 1.
  430.  
  431. @item prefer_column_vectors
  432. @xref{Index Expressions}.
  433.  
  434. Default value: 0.
  435.  
  436. @item print_answer_id_name
  437. @xref{Terminal Output}.
  438.  
  439. Default value: 1.
  440.  
  441. @item print_empty_dimensions
  442. @xref{Empty Matrices}.
  443.  
  444. Default value: 1.
  445.  
  446. @item resize_on_range_error
  447. @xref{Index Expressions}.
  448.  
  449. Default value: 1.
  450.  
  451. @item return_last_computed_value
  452. @xref{Returning From a Function}.
  453.  
  454. Default value: 0.
  455.  
  456. @item save_precision
  457. @xref{Simple File I/O}.
  458.  
  459. Default value: 17.
  460.  
  461. @item saving_history
  462. @xref{Commands For History}.
  463.  
  464. Default value: 1.
  465.  
  466. @item silent_functions
  467. @xref{Defining Functions}.
  468.  
  469. Default value: 0.
  470.  
  471. @item split_long_rows
  472. @xref{Matrices}.
  473.  
  474. Default value: 1.
  475.  
  476. @item struct_levels_to_print
  477. @xref{Data Structures}.
  478.  
  479. Default value: 2.
  480.  
  481. @item suppress_verbose_help_message
  482. @xref{Getting Help}.
  483.  
  484. Default value: 1.
  485.  
  486. @item treat_neg_dim_as_zero
  487. @xref{Special Utility Matrices}.
  488.  
  489. Default value: 0.
  490.  
  491. @item warn_assign_as_truth_value
  492. @xref{The if Statement}.
  493.  
  494. Default value: 1.
  495.  
  496. @item warn_comma_in_global_decl
  497. @xref{Global Variables}.
  498.  
  499. Default value: 1.
  500.  
  501. @item warn_divide_by_zero
  502. @xref{Arithmetic Ops}.
  503.  
  504. Default value: 1.
  505.  
  506. @item warn_function_name_clash
  507. @xref{Function Files}.
  508.  
  509. Default value: 1.
  510.  
  511. @item whitespace_in_literal_matrix
  512. @xref{Matrices}.
  513.  
  514. Default value: @code{""}.
  515. @end vtable
  516.  
  517.  
  518. @node Defaults from the Environment,  , Summary of Built-in Variables, Variables
  519. @section Defaults from the Environment
  520.  
  521. Octave uses the values of the following environment variables to set the
  522. default values for the corresponding built-in variables.  In addition,
  523. the values from the environment may be overridden by command-line
  524. arguments.  @xref{Command Line Options}.
  525.  
  526. @vtable @code
  527. @item EDITOR
  528. @xref{Commands For History}.
  529.  
  530. Built-in variable: @code{EDITOR}.
  531.  
  532. @item OCTAVE_EXEC_PATH        
  533. @xref{Controlling Subprocesses}.
  534.  
  535. Built-in variable: @code{EXEC_PATH}.
  536. Command-line argument: @code{--exec-path}.
  537.  
  538. @item OCTAVE_PATH
  539. @xref{Function Files}.
  540.  
  541. Built-in variable: @code{LOADPATH}.
  542. Command-line argument: @code{--path}.
  543.  
  544. @item OCTAVE_INFO_FILE
  545. @xref{Getting Help}.
  546.  
  547. Built-in variable: @code{INFO_FILE}.
  548. Command-line argument: @code{--info-file}.
  549.  
  550. @item OCTAVE_INFO_PROGRAM
  551. @xref{Getting Help}.
  552.  
  553. Built-in variable: @code{INFO_PROGRAM}.
  554. Command-line argument: @code{--info-program}.
  555.  
  556. @item OCTAVE_HISTSIZE
  557. @xref{Commands For History}.
  558.  
  559. Built-in variable: @code{history_size}.
  560.  
  561. @item OCTAVE_HISTFILE
  562. @xref{Commands For History}.
  563.  
  564. Built-in variable: @code{history_file}.
  565. @end vtable
  566.